package com.example.shushiyunsst.fragment.main.wallet.item_provider

import android.view.View
import android.view.ViewGroup
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.framework.recycler.multi22.Basic.JssBaseItemProvider
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.miner22.CMLCollectionFragment
import com.example.shushiyunsst.fragment.main.home.child.miner22.CMLTransferFragment
import com.example.shushiyunsst.fragment.main.home.child.miner22.bean.CMLIntent
import com.example.shushiyunsst.fragment.main.wallet.bean.MyCaptureInterfaces
import com.example.shushiyunsst.fragment.main.wallet.bean.MyCaptureListBean
import com.example.shushiyunsst.fragment.main.wallet.sub.*
import com.example.wallet.db.UserInfoManager
import com.example.wallet.global.AppGlobals

class MyCaptureListProvider : JssBaseItemProvider<MyCaptureInterfaces?, JssBaseViewHolder>() {


    override val layoutId: Int = R.layout.my_capture_fragment_item_layout


    override val itemViewType: Int = MyCaptureInterfaces.List

    private val sst = AppGlobals.getApplication().getString(R.string.wallet_sst)

    private val cml = AppGlobals.getApplication().getString(R.string.wallet_cml)

    private val arl = AppGlobals.getApplication().getString(R.string.wallet_arl)


    override fun onViewHolderCreated(viewHolder: JssBaseViewHolder, viewType: Int) {
        super.onViewHolderCreated(viewHolder, viewType)
        addChildClickViewIds(
            R.id.transferTv, R.id.collectionsTv,
            R.id.rechargeTv, R.id.withdrawTv,
            R.id.detail, R.id.mExchangePass
        )
    }

    override fun convert(helper: JssBaseViewHolder?, mItem: MyCaptureInterfaces?) {

        val item = mItem as MyCaptureListBean


        var availableTvStr = ""

        var convertToCNYStr = ""

        if (item.iswelfare == 1) {
            availableTvStr = "已释放： ${item.releasedStr}"
            convertToCNYStr = "未释放： ${item.notReleasedStr}"
        } else {
            availableTvStr = "可用: ${item?.balanceStr}"
            convertToCNYStr = "折合(CNY): ${item?.balanceCnyStr}"
        }

        val operationVisible =
            item.isSupportTrans == 0
                    && item.isSupportTrans == 0
                    && item.isExchangePass == 0
                    && item.isSupportReceive == 0

        helper?.setText(R.id.moneyTv, item?.title)
            ?.setText(R.id.availableTv, availableTvStr)
            ?.setText(R.id.convertToCNY, convertToCNYStr)
            ?.setViewVisible(R.id.detail, item?.iswelfare == 1)
            ?.setViewVisible(R.id.transferFrameTv, item?.isSupportTrans == 1)
            ?.setViewVisible(R.id.mExchangeFramePass, item?.isExchangePass == 1)
            ?.setViewVisible(R.id.collectionsFrameTv, item?.isSupportReceive == 1)
            ?.setGone(R.id.operation, operationVisible)
    }

    override fun onChildClick(
        helper: BaseViewHolder,
        view: View,
        data: MyCaptureInterfaces?,
        position: Int
    ) {
        super.onChildClick(helper, view, data, position)

        val attachFragment = getAdapter()?.getAttachFragment()

        val item = getAdapter()?.getItem(position)as MyCaptureListBean

        when (view.id) {
            R.id.mExchangePass -> {
                attachFragment?.start(PassCardExchangeFragment.newInstance(item))
            }
            R.id.detail -> {
                if (item.iswelfare == 1) {
                    attachFragment?.start(WelfareFragment.newInstance())
                }
            }
            R.id.transferTv -> {
                //转账
                if (item.isExchangePass == 1) {
                    attachFragment?.start(ExchangePassTransferFragment.newInstance(item))
                    return
                }
                transferHand(item)

            }
            R.id.collectionsTv -> {
                //收款
                if (item.isExchangePass == 1) {
                    attachFragment?.start(CollectionsFragment.newInstance(item))
                    return
                }
                collectionsHand(item)
            }
            R.id.rechargeTv -> {
                //充币
                if (item.tokenType == sst) {
                    attachFragment?.start(RechargeFragment.newInstance())
                }

            }
            R.id.withdrawTv -> {
                //提币
                if (item.tokenType == sst) {
                    attachFragment?.start(WithdrawFragment.newInstance())
                }

            }
        }
    }

    //转账
    private fun transferHand(item: MyCaptureListBean) {
        val attachFragment = getAdapter()?.getAttachFragment()
        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
        val mAddress = userInfo?.sysWalletAddress ?: ""
        when (item.tokenType) {
            sst -> {
//                start(TransferFragment.newInstance())
                attachFragment?.start(ExchangePassTransferFragment.newInstance(item))
            }
            cml, arl -> {
                val balance = "${item.balance}"


                val icon = item.tokenType

                val cmlIntent = CMLIntent(
                    mAddress,
                    balance,
                    icon,
                    icon,
                    CMLIntent.PARENT_ACCOUNT
                )
                cmlIntent.balanceCny = item.balanceCny.toString()
                attachFragment?.start(CMLTransferFragment.newInstance(cmlIntent))
            }
        }
    }

    //收款
    private fun collectionsHand(item: MyCaptureListBean) {
        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
        val mAddress = userInfo?.sysWalletAddress ?: ""
        val attachFragment = getAdapter()?.getAttachFragment()
        when (item.tokenType) {
            sst -> {
                attachFragment?.start(CollectionsFragment.newInstance())
            }
            cml, arl -> {
                val balance = "${item.balance}"

                val icon = item.tokenType
                val cmlIntent = CMLIntent(
                    mAddress,
                    balance,
                    icon,
                    icon,
                    CMLIntent.PARENT_ACCOUNT
                )
                cmlIntent.balanceCny = item.balanceCny.toString()
                attachFragment?.start(CMLCollectionFragment.newInstance(cmlIntent))
            }

            else -> attachFragment?.start(CollectionsFragment.newInstance())
        }
    }
}